Completed
Pull Request — future (#197)
by Xaver
46s
created

node.js ➔ ... ➔ setData   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 6
rs 9.4285
nop 1
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper'],
2
  function (SortTable, V, d3Interpolate, moment, helper) {
3
    'use strict';
4
    V = V.default;
5
6
    function showGeoURI(d) {
7
      if (!helper.hasLocation(d)) {
8
        return undefined;
9
      }
10
11
      return V.h('td',
12
        V.h('a',
13
          { props: { href: 'geo:' + d.location.latitude + ',' + d.location.longitude } },
14
          Number(d.location.latitude.toFixed(6)) + ', ' + Number(d.location.longitude.toFixed(6))
15
        )
16
      );
17
    }
18
19
    function showStatus(d) {
20
      return V.h('td',
21
        { props: { className: d.is_online ? 'online' : 'offline' } },
22
        _.t((d.is_online ? 'node.lastOnline' : 'node.lastOffline'), {
23
          time: d.lastseen.fromNow(),
24
          date: d.lastseen.format('DD.MM.YYYY, H:mm:ss')
25
        }));
26
    }
27
28
    function showFirmware(d) {
29
      return [
30
        helper.dictGet(d, ['firmware', 'release']),
31
        helper.dictGet(d, ['firmware', 'base'])
32
      ].filter(function (n) {
33
        return n !== null;
34
      }).join(' / ') || undefined;
35
    }
36
37
    function showSite(d, config) {
38
      var rt = d.site_code;
39
      if (config.siteNames) {
40
        config.siteNames.forEach(function (t) {
41
          if (d.site_code === t.site) {
42
            rt = t.name;
43
          }
44
        });
45
      }
46
      return rt;
47
    }
48
49
    function showClients(d) {
50
      if (!d.is_online) {
51
        return undefined;
52
      }
53
54
      var clients = [
55
        V.h('span', [
56
          d.clients > 0 ? d.clients : _.t('none'),
57
          V.h('br'),
58
          V.h('i', { props: { className: 'ion-people', title: _.t('node.clients') } })
59
        ]),
60
        V.h('span',
61
          { props: { className: 'legend-24ghz' } },
62
          [
63
            d.clients_wifi24,
64
            V.h('br'),
65
            V.h('span', { props: { className: 'symbol', title: '2,4 Ghz' } })
66
          ]),
67
        V.h('span',
68
          { props: { className: 'legend-5ghz' } },
69
          [
70
            d.clients_wifi5,
71
            V.h('br'),
72
            V.h('span', { props: { className: 'symbol', title: '5 Ghz' } })
73
          ]),
74
        V.h('span',
75
          { props: { className: 'legend-others' } },
76
          [
77
            d.clients_other,
78
            V.h('br'),
79
            V.h('span', { props: { className: 'symbol', title: _.t('others') } })
80
          ])
81
      ];
82
83
      return V.h('td', { props: { className: 'clients' } }, clients);
84
    }
85
86
    function showIPs(d) {
87
      var string = [];
88
      var ips = d.network.addresses;
89
      ips.sort();
90
      ips.forEach(function (ip, i) {
91
        if (i > 0) {
92
          string.push(V.h('br'));
93
        }
94
95
        if (!ip.startsWith('fe80:')) {
96
          string.push(V.h('a', { props: { href: 'http://[' + ip + ']/', target: '_blank' } }, ip));
97
        } else {
98
          string.push(ip);
99
        }
100
      });
101
      return V.h('td', string);
102
    }
103
104
    function showBar(v, width, warning) {
105
      return V.h('span',
106
        { props: { className: 'bar' + (warning ? ' warning' : '') } },
107
        [
108
          V.h('span',
109
            {
110
              style: { width: (width * 100) + '%' }
111
            }),
112
          V.h('label', v)
113
        ]
114
      );
115
    }
116
117
    function showLoad(d) {
118
      if (!('loadavg' in d)) {
119
        return undefined;
120
      }
121
      return showBar(d.loadavg.toFixed(2), d.loadavg % 1, d.loadavg >= d.nproc);
122
    }
123
124
    function showRAM(d) {
125
      if (!('memory_usage' in d)) {
126
        return undefined;
127
      }
128
      return showBar(Math.round(d.memory_usage * 100) + ' %', d.memory_usage, d.memory_usage >= 0.8);
129
    }
130
131
    function showAutoupdate(d) {
132
      return d.autoupdater.enabled ? _.t('node.activated', { branch: d.autoupdater.branch }) : _.t('node.deactivated');
133
    }
134
135
    function showStatImg(o, d) {
136
      var subst = {};
137
      subst['{NODE_ID}'] = d.node_id;
138
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
139
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
140
      subst['{LOCALE}'] = _.locale();
141
      return helper.showStat(V, o, subst);
142
    }
143
144
    return function (config, el, router, d, linkScale, nodeDict) {
145
      function nodeLink(node) {
146
        return V.h('a', {
147
          props: {
148
            className: node.is_online ? 'online' : 'offline',
149
            href: router.generateLink({ node: node.node_id })
150
          }, on: {
151
            click: function (e) {
152
              router.fullUrl({ node: node.node_id }, e);
153
            }
154
          }
155
        }, node.hostname);
156
      }
157
158
      function nodeIdLink(nodeId) {
159
        if (nodeDict[nodeId]) {
160
          return nodeLink(nodeDict[nodeId]);
161
        }
162
        return nodeId;
163
      }
164
165
      function showGateway(node) {
166
        var gatewayCols = [
167
          V.h('span', [
168
            nodeIdLink(node.gateway_nexthop),
169
            V.h('br'),
170
            _.t('node.nexthop')
171
          ]),
172
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
173
          V.h('span', [
174
            nodeIdLink(node.gateway),
175
            V.h('br'),
176
            'IPv4'
177
          ]),
178
          V.h('span', [
179
            nodeIdLink(node.gateway6),
180
            V.h('br'),
181
            'IPv6'
182
          ])
183
        ];
184
185
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
186
      }
187
188
      function renderNeighbourRow(n) {
189
        var icons = '';
190
        if (helper.hasLocation(n.node)) {
191
          icons = V.h('span', { props: { className: 'ion-location' } });
192
        }
193
194
        return V.h('tr', [
195
          V.h('td', icons),
196
          V.h('td', nodeLink(n.node)),
197
          V.h('td', n.node.clients),
198
          V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq)),
199
          V.h('td', helper.showDistance(n.link))
200
        ]);
201
      }
202
203
      var self = this;
204
      var header = document.createElement('h2');
205
      var table = document.createElement('table');
206
      var images = document.createElement('div');
207
      var neighbours = document.createElement('h3');
208
      var headings = [{
209
        name: ''
210
      }, {
211
        name: 'node.nodes',
212
        sort: function (a, b) {
213
          return a.node.hostname.localeCompare(b.node.hostname);
214
        },
215
        reverse: false
216
      }, {
217
        name: 'node.clients',
218
        class: 'ion-people',
219
        sort: function (a, b) {
220
          return a.node.clients - b.node.clients;
221
        },
222
        reverse: true
223
      }, {
224
        name: 'node.tq',
225
        class: 'ion-connection-bars',
226
        sort: function (a, b) {
227
          return a.link.source_tq - b.link.source_tq;
228
        },
229
        reverse: true
230
      }, {
231
        name: 'node.distance',
232
        class: 'ion-arrow-resize',
233
        sort: function (a, b) {
234
          return (a.link.distance === undefined ? -1 : a.link.distance) -
235
            (b.link.distance === undefined ? -1 : b.link.distance);
236
        },
237
        reverse: true
238
      }];
239
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
240
241
      el.appendChild(header);
242
      el.appendChild(table);
243
      el.appendChild(neighbours);
244
      el.appendChild(tableNeighbour.el);
245
      el.appendChild(images);
246
247
      self.render = function render() {
248
        V.patch(header, V.h('h2', d.hostname));
249
250
        var children = [];
251
252
        children.push(helper.attributeEntry(V, 'node.status', showStatus(d)));
253
        children.push(helper.attributeEntry(V, 'node.gateway', d.is_gateway ? 'ja' : undefined));
254
        children.push(helper.attributeEntry(V, 'node.coordinates', showGeoURI(d)));
255
256
        if (config.nodeInfobox && config.nodeInfobox.contact) {
257
          children.push(helper.attributeEntry(V, 'node.contact', helper.dictGet(d, ['owner', 'contact'])));
258
        }
259
260
        children.push(helper.attributeEntry(V, 'node.hardware', d.model));
261
        children.push(helper.attributeEntry(V, 'node.primaryMac', d.network.mac));
262
        children.push(helper.attributeEntry(V, 'node.firmware', showFirmware(d)));
263
        children.push(helper.attributeEntry(V, 'node.site', showSite(d, config)));
264
        children.push(helper.attributeEntry(V, 'node.uptime', moment.utc(d.uptime).local().fromNow(true)));
265
        children.push(helper.attributeEntry(V, 'node.firstSeen', d.firstseen.fromNow(true)));
266
        if (config.nodeInfobox && config.nodeInfobox.hardwareUsage) {
267
          children.push(helper.attributeEntry(V, 'node.systemLoad', showLoad(d)));
268
          children.push(helper.attributeEntry(V, 'node.ram', showRAM(d)));
269
        }
270
        children.push(helper.attributeEntry(V, 'node.ipAddresses', showIPs(d)));
271
        children.push(helper.attributeEntry(V, 'node.update', showAutoupdate(d)));
272
        children.push(helper.attributeEntry(V, 'node.clients', showClients(d)));
273
        children.push(helper.attributeEntry(V, 'node.gateway', showGateway(d)));
274
275
        var elNew = V.h('table', children);
276
        table = V.patch(table, elNew);
277
        table.elm.classList.add('attributes');
278
279
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
280
        if (d.neighbours.length > 0) {
281
          tableNeighbour.setData(d.neighbours);
282
          tableNeighbour.el.elm.classList.add('node-links');
283
        }
284
285
        if (config.nodeInfos) {
286
          var img = [];
287
          config.nodeInfos.forEach(function (nodeInfo) {
288
            img.push(V.h('h4', nodeInfo.name));
289
            img.push(showStatImg(nodeInfo, d));
290
          });
291
          images = V.patch(images, V.h('div', img));
292
        }
293
      };
294
295
      self.setData = function setData(data) {
296
        if (data.nodeDict[d.node_id]) {
297
          d = data.nodeDict[d.node_id];
298
        }
299
        self.render();
300
      };
301
      return self;
302
    };
303
  });
304